<?php


namespace app\util;


class Tools
{
    /**
     * 密码加密方法
     *
     * @param $str
     * @param string $auth_key
     * @return string
     */
    public static function userMd5($str,$auth_key=''){
        if (!$auth_key){
            $auth_key = config('antadmin.AUTH_KEY');
        }
        return ''===$str?'':md5(sha1($str).$auth_key);
    }

    /**
     * 判断当前用户是否是超级管理员
     * @param string $uid
     * @return bool
     */
    public static function isAdministrator($uid =''){
        if (!empty($uid)){
            $adminConf = config('antadmin.USER_ADMINISTRATOR');
            if (is_array($adminConf)){
                if (is_array($uid)){
                    $m = array_intersect($adminConf,$uid);
                    if (count($m)){
                        return true;
                    }
                }else{
                    if (in_array($uid,$adminConf)){
                        return true;
                    }
                }
            }else{
                if (is_array($uid)){
                    if (in_array($adminConf,$uid)){
                        return true;
                    }
                }else{
                    if ($uid == $adminConf){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 将查询的二维对象转换成二维数组
     * @param $res
     * @param string $key
     * @return array
     */
    public static function buildArrFromObj($res,$key=''){
        $arr = [];
        foreach ($res as $value) {
            $value = $value->toArray();
            if ($key) {
                $arr[$value[$key]] = $value;
            } else {
                $arr[] = $value;
            }
        }

        return $arr;
    }


}
